package com.yld.demo2.kafka;

import com.alibaba.fastjson.JSONObject;
import com.yld.demo2.entity.vo.OrderListVo;
import com.yld.demo2.service.OrderListService;
import com.yld.demo2.util.DataUtil;
import com.yld.demo2.util.tooltips.ResBean;
import io.swagger.annotations.ApiOperation;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class KafkaConsumer {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OrderListService orderListService;

    @KafkaListener(topics = {"delete-kafka"})
    public void deleteConsumer(ConsumerRecord<?, String> record) {
        int i = Integer.parseInt(record.value());
        System.out.println(i);
        //取到userId
        Integer userid = orderListService.selectUserIdByOrderNo(i);

        //清除redis
        redisTemplate.opsForList().getOperations().delete("userId=" + userid);

        orderListService.deleteByOrderNo(i);
    }

    @KafkaListener(topics = {"insert-kafka"})
    public void insertCunsumer(ConsumerRecord<?,?> record){
        System.out.println("监听到insert-kafka=========================");
        OrderListVo orderListVo = JSONObject.toJavaObject(JSONObject.parseObject(record.value().toString()), OrderListVo.class);
        System.out.println("orderListVo=================="+ orderListVo.toString());

        Boolean aBoolean = orderListService.insertAll(orderListVo);
        System.out.println(aBoolean);

    }

    @KafkaListener(topics = {"update-kafka"})
    public void updateCunsumer(ConsumerRecord<?,?> record){
        System.out.println("监听到update-kafka=========================");
        OrderListVo orderListVo = JSONObject.toJavaObject(JSONObject.parseObject(record.value().toString()), OrderListVo.class);
        redisTemplate.opsForList().getOperations().delete("userId="+orderListVo.getUserid());

        System.out.println("orderListVo=================="+ orderListVo.toString());
        Boolean aBoolean = orderListService.updateByOrderNo(orderListVo);
        System.out.println(aBoolean);

    }


}
